package com.tz.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.config.RedisConfig;
import com.tz.farmer.mapper.BusinessInfoMapper;
import com.tz.farmer.mapper.GoodsInfoMapper;
import com.tz.farmer.model.BusinessInfo;
import com.tz.farmer.model.FarmerKnowledge;
import com.tz.farmer.model.GoodsInfo;
import com.tz.farmer.service.BusinessService;
import com.tz.farmer.so.BusinessPushSo;
import com.tz.farmer.so.GoodsSo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessInfoMapper, BusinessInfo>  implements BusinessService {
    private static Logger logger = LoggerFactory.getLogger(KnowledgeServiceImpl.class);

    @Autowired
    BusinessInfoMapper businessInfoMapper;

    @Autowired
    GoodsInfoMapper goodsInfoMapper;

    @Override
    public BusinessInfo saveBusinessInfo(BusinessPushSo businessPushSo) throws Exception{

        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setBusinessPersonName(businessPushSo.getBusinessPersonName());
        businessInfo.setBusinessName(businessPushSo.getBusinessName());
        businessInfo.setBusinessAddress(businessPushSo.getBusinessAddress());
        businessInfo.setUpdateTime(LocalDateTime.now());
        businessInfo.setUniscId(businessPushSo.getUniscId());
        //插入商户数据
        businessInfoMapper.insert(businessInfo);
        return businessInfo;
    }

    @Override
    public ResultEx getBusinessInfo(BusinessPushSo businessPushSo) throws Exception{
        Map map=new HashMap<>();
        //商户信息
        LambdaQueryWrapper<BusinessInfo> incQueryWrapper = Wrappers.lambdaQuery();
        incQueryWrapper.eq(BusinessInfo::getUniscId, businessPushSo.getUniscId());
        BusinessInfo businessInfo=businessInfoMapper.selectOne(incQueryWrapper);
        //商品信息
        List<GoodsSo> goodsInfos = goodsInfoMapper.selectGoodsInfos(businessPushSo.getGoodsId());
        //更新后台扫码次数/当日访问次数
        BusinessInfo business=BusinessInfo.builder().scanNum(businessInfo.getScanNum()+1).visitCount(businessInfo.getVisitCount()+1).id(businessInfo.getId()).build();
        businessInfoMapper.updateById(business);
        logger.info("更新查看次数完成===============");
        map.put("business",businessInfo);
        map.put("goods",goodsInfos);
        return ResultEx.builder().makeSuccessResult(map);
    }

    @Override
    public Boolean checkBusinessInfo(BusinessPushSo businessPushSo) throws Exception{
        //根据社会统一代码查询商户信息
        LambdaQueryWrapper<BusinessInfo> incQueryWrapper = Wrappers.lambdaQuery();
        incQueryWrapper.eq(BusinessInfo::getUniscId, businessPushSo.getUniscId());
        BusinessInfo businessInfo=businessInfoMapper.selectOne(incQueryWrapper);
        if(businessInfo==null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean checkGoodsInfo(List<GoodsInfo> list) throws Exception{
        GoodsInfo goodsInfo=list.get(0);
        //根据商品id查询商品信息
        LambdaQueryWrapper<GoodsInfo> incQueryWrapper = Wrappers.lambdaQuery();
        incQueryWrapper.eq(GoodsInfo::getGoodsId, goodsInfo.getGoodsId()).eq(GoodsInfo::getBatchInfo,goodsInfo.getBatchInfo());
        GoodsInfo goods=goodsInfoMapper.selectOne(incQueryWrapper);
        if(goods==null){
            return true;
        }else{
            return false;
        }
    }
}
